package com.jiuwu.spring.eshop.controller;

import com.jiuwu.spring.eshop.db.entity.SuperType;
import com.jiuwu.spring.eshop.respository.SuperTypeRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Optional;

@Slf4j
@Controller
@RequestMapping("/manage")
public class SuperTypeController {

    @Autowired
    SuperTypeRepository superTypeRepository;

    /**
     * 获取大类信息
     */
    @RequestMapping("/superType")
    public String getSuperType(Model model){
       List<SuperType> superTypeList= superTypeRepository.findAll();
       model.addAttribute("superTypeList",superTypeList);
       return "/manage/superType";

    }

    @RequestMapping("/super_add")
    public String loadSuperType(){
        return "/manage/super_add";
    }

    /**
     * 添加大类
     */
    @RequestMapping("/superType_add_deal")
    public String addSuperType(@RequestParam("typename") String superTypename) {
        SuperType superType = new SuperType();
        superType.setTypeName(superTypename);
        superTypeRepository.save(superType);
        return "redirect:/manage/index?id=5";
    }

    /**
     * 删除大类
     */
    @RequestMapping("/superType_del_deal")
    public String delSuperType(HttpServletRequest request){
        for (String delid : request.getParameterValues("delid")) {
            Integer id = Integer.valueOf(delid);
            superTypeRepository.deleteById(id);

        }

        return "redirect:/manage/index?id=5";
    }














    //-------------------------thymeleaf----------------------------------------

    //获取所有大分类
    @GetMapping("/supers")
    public String getSuper(Model model){
        List<SuperType> superTypeList= superTypeRepository.findAll();
        model.addAttribute("superTypeList",superTypeList);
        return "goods/superType";

    }

    //进入大分类修改页面，get请求
    @GetMapping("/super/{superId}")
    public String toAddSuperType(@PathVariable("superId") Integer id,Model model) {
        Optional<SuperType> superTypeOptional = superTypeRepository.findById(id);
        SuperType superType =null;
        if(superTypeOptional.isPresent()){
            superType=superTypeOptional.get();
        }
        model.addAttribute("super",superType);
        return "goods/editSuperType";
    }

    //添加大分类， post请求
    @PostMapping("/super")
    public String addSuperType2(@RequestParam("typeName") String superName) {
        SuperType superType = new SuperType();
        superType.setTypeName(superName);
        superTypeRepository.save(superType);
        return "redirect:/manage/supers";  //重定向/转发可以发送请求
    }


    //修改信息 put请求
    @PostMapping("/updateSuper")
    public String updateSuperType(SuperType superType){
        System.out.println("分类数据："+superType);
        superTypeRepository.save(superType);
        return "redirect:/manage/supers";
    }

    @GetMapping("/delSuperType/{superId}")
    public String delSuperType(@PathVariable("superId") Integer id) {
        superTypeRepository.deleteById(id);
        return "redirect:/manage/supers";
    }

}
